<template>
  <el-dialog :visible.sync="visibleP" :before-close="handleClose" :close-on-click-modal="false">
     <header slot="title">
      <span class="title">
        {{getTitle}}
      </span>
    </header>
    <section style="padding-left:200px">
      <verify :captcha.sync="captcha" :preCode.sync="preCode" :getPhone.sync="phoneList" :veriFi="this.detailsP.masterChainId"></verify>
    </section>
    <footer slot="footer">
        <el-button round @click="handleSubmit" type="primary" >确认</el-button>
    </footer>
  </el-dialog>
</template>
<style scoped>
footer {
  text-align: center;
}
</style>

<script>
import DialogClose from '@/mixins/suplier/Ar/DialogClose'
import Common from '@/mixins/common/common'
import { debounce, erroShow } from '@/util/util' // 防抖函数
import { loadingConf } from '@/config/common' // 获取加载配置
import mixVerify from '@/mixins/common/dialogContract'
import {YZTUrl} from '@/config/env'
/* 合同确认 */
export default {
  props: ['visibleP', 'detailsP'],
  mixins: [DialogClose, Common, mixVerify],
  data () {
    return {
      checkList: [],
      isLoading: false
    }
  },
  destroyed () {
    console.log('销毁')
  },
  methods: {
    handleSubmit: debounce(submit, 1000, {
      'leading': true,
      'trailing': false
    }),
    init: Init,
    // 获取供应商代码
    getVendorCode () {
      this.axios.post('/commonCust/queryCustomer.do', {
        custId: this.detailsP.custFromId,
        companyName: ''
      }).then(res => {
        if (res.data.status) {
          this.getOneCommom(res.data.data.vendorCode)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 调用一账通的
    getOneCommom (arg) {
      this.axios.post(YZTUrl + '/rest/track/channelTrack', {
        channel: 'JX',
        trackType: 'BLFK',
        vendorCode: arg
      }, {headers: {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
      }
      }).then(res => {
        if (res.data.status) {
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 调用微信方法发送消息
    sendTradeMessage (transSerialNo) {
      this.axios.post('/wechat/juxin/sendJuXinTransactionMessage', {
        transSerialNo: transSerialNo,
        loanType: 3,
        operateTypeIndex: 8
      }).then(res => {
        console.log(res)
      }).catch(err => {
        console.log(err)
      })
    }
  },
  computed: {
    getTitle () {
      return this.detailsP.masterChainId + '合同签署'
    },
    phoneList () {
      return this.detailsP.phone
    }
  }
}
function submit () {
  if (this.captcha.length === 0) {
    this.$message({
      type: 'error',
      message: '验证码不能为空'
    })
    return
  }
  // 显示加载图标
  const loading = this.$loading(loadingConf.sub())
  this.axios.post('/loan2/confirmInitiateSigning.do', { masterChainId: this.detailsP.masterChainId, verificationCode: this.preCode + this.captcha }).then(res => {
    let type = res.data.status ? 'success' : 'error'
    this.$message({
      message: res.data.msg ? res.data.msg : '返回结果错误，请联系管理员',
      type: type
    })
    // 操作成功 关闭弹窗
    if (res.data.status) {
      // 调用微信接口
      this.sendTradeMessage(res.data.data)
      this.handleClose() // 关闭弹窗
      this.$parent.fresh() // 刷新数据
      this.getVendorCode() // 调用获取供应商代码
    } else {
      loading.close()
    }
  }).catch((err) => {
    // 错误提示
    erroShow.call(this, err, loading)
  })
}
// 初始化
function Init () {
  this.captcha = ''
  if (this.$refs.verify) {
    this.$refs.verify.init()
  }
}
</script>
